package com.qfedu.vhr.employee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.qfedu.vhr.common.MailBody;
import com.qfedu.vhr.common.RespBean;
import com.qfedu.vhr.employee.entity.Employee;
import com.qfedu.vhr.employee.entity.MailSendLog;
import com.qfedu.vhr.employee.mapper.EmployeeMapper;
import com.qfedu.vhr.employee.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.vhr.employee.service.IMailSendLogService;
import com.qfedu.vhr.framework.config.RabbitConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-02-08
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    IMailSendLogService mailSendLogService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 1. 首先把用户添加到数据表中。
     * 2. 在邮件发送之前，先把邮件发送的关键信息存入到数据表中
     *   a. 邮件发送时的交换机、队列名、邮件发送的状态、员工 ID。。。
     * 3. 发送邮件
     * @param employee
     * @return
     */
    @Override
    public RespBean addEmployee(Employee employee) {
        try {
            save(employee);
            MailSendLog mailSendLog = new MailSendLog();
            //这条消息的唯一 ID
            String msgId = UUID.randomUUID().toString();
            mailSendLog.setMsgId(msgId);
            //重试次数默认为 0
            mailSendLog.setCount(0);
            //消息状态 为 0，表示消息处于发送中
            mailSendLog.setStatus(0);
            mailSendLog.setCreateTime(LocalDateTime.now());
            //tryTime 是指重试时间
            //这条消息发送出去之后，30s 之内，即使status为0，也不用重试，不用重新发送，30s 之后，如果 status 还是 0，则需要进行重试了
            mailSendLog.setTryTime(LocalDateTime.now().plusSeconds(30));
            mailSendLog.setEmpId(employee.getId());
            mailSendLog.setRouteKey(RabbitConfig.EMPLOYEE_MAIL_QUEUE_NAME);
            mailSendLog.setExchange(RabbitConfig.EMPLOYEE_MAIL_EXCHANGE_NAME);
            mailSendLogService.save(mailSendLog);
            MailBody mailBody = new MailBody();
            mailBody.setContent("xxxxx");
            mailBody.setMail(employee.getEmail());
            mailBody.setMsgId(msgId);
            rabbitTemplate.convertAndSend("RabbitConfig.EMPLOYEE_MAIL_EXCHANGE_NAME", RabbitConfig.EMPLOYEE_MAIL_QUEUE_NAME, mailBody,new CorrelationData(msgId));
            return RespBean.ok("添加成功");
        } catch (AmqpException e) {
            return RespBean.error("添加失败");
        }
    }

    @Override
    public RespBean modify(Employee employee) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Employee::getName,employee.getName()).eq(Employee::getIdCard,employee.getIdCard());
        Employee one = getOne(queryWrapper);
        if (one != null){
            return RespBean.error("姓名或身份证编码重复");
        }

        return updateById(employee) ? RespBean.ok("更新成功") : RespBean.error("更新失败");
    }
}
